package cn.bocweb.spinning.view.forun;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CompanyList1Bean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.view.activity.LoginAcivity;


public class ForunMainFragment extends Fragment implements View.OnClickListener {

    BBSActivity mainActivity;
    MainForunAdapter mainForunAdapter;
    UserPresenter presenter;
    List<CompanyList1Bean> list;
    String userid;
    UserBean.DataEntity dataEntity;

    @Bind(R.id.forun_select)
    RadioGroup forunSelect;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.main_forun_viewpager)
    ViewPager mainForunViewpager;
    @Bind(R.id.bbs_main_hot)
    RadioButton bbsMainHot;
    @Bind(R.id.bbs_main_special)
    RadioButton bbsMainSpecial;
    @Bind(R.id.bbs_main_social)
    RadioButton bbsMainSocial;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_forun_main, container, false);

        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        dataEntity = new UserBean.DataEntity();
        list = new ArrayList<>();
        mainActivity = (BBSActivity) getActivity();
        mainForunAdapter = new MainForunAdapter(getChildFragmentManager());
        presenter = new UserPresenterImp();
        bbsMainHot.setChecked(true);

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(getActivity());
        userid = dataEntity.getUserid();

        initToolbar();
        initViewPager();
        initClickBtn();
        initRadioBtn();

    }

    private void initRadioBtn() {
        forunSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.bbs_main_hot: {
                        mainForunViewpager.setCurrentItem(0);
                        break;
                    }
                    case R.id.bbs_main_special: {
                        mainForunViewpager.setCurrentItem(1);
                        break;
                    }
                    case R.id.bbs_main_social: {
                        mainForunViewpager.setCurrentItem(2);
                        break;
                    }
                }
            }
        });
    }

    private void initViewPager() {

        mainForunViewpager.setAdapter(mainForunAdapter);
        mainForunViewpager.setOffscreenPageLimit(2);
        mainForunViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0: {
                        bbsMainHot.setChecked(true);
                        break;
                    }
                    case 1: {
                        bbsMainSpecial.setChecked(true);
                        break;
                    }
                    case 2: {
                        bbsMainSocial.setChecked(true);
                        break;
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
    }

    private void initClickBtn() {
    }

    private void initToolbar() {
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainActivity.finish();
            }
        });

        Menu menu = toolbar.getMenu();
        menu.add(Menu.NONE, 0, Menu.NONE, "发表话题");
        menu.add(Menu.NONE, 1, Menu.NONE, "我的话题");

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                dataEntity.readInfoFromLocal(getContext());
                Intent intent;
                switch (item.getItemId()) {
                    case 1: {
                        if (TextUtils.isEmpty(dataEntity.getUserid())) {
                            intent = new Intent(getContext(), LoginAcivity.class);
                            startActivity(intent);
                        } else {
                            intent = new Intent(getActivity(), ForunMyTopicActivity.class);
                            startActivity(intent);
                        }
                        break;
                    }
                    case 0: {
                        if (TextUtils.isEmpty(dataEntity.getUserid())) {
                            intent = new Intent(getContext(), LoginAcivity.class);
                            startActivity(intent);
                        } else {
                            intent = new Intent(getActivity(), ForunSendTopicActivity.class);
                            startActivity(intent);
                        }
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
